package com.hfzy.ihk.web.ivr.support;

import com.hfzy.ihk.web.ivr.constant.RedisKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * Created by czs on 2019/6/13.
 * 轮询选择主显号工具类
 *
 */
@Component("selectShowNum")
@Slf4j
public class SelectShowNumSupport {



    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * 根据城市获取主显号
     * @param city
     * @return
     */
    public  synchronized String getCopShowNum(String city){

        if(city==null||"-1".equals(city))
            city="1"; //没有默认一个广州
        String listKey = RedisKey.CUCC_SHOW_NUM_LIST+city;
        String indexKey = RedisKey.CUCC_SHOW_NUM_INDEX +city;

        Long size = redisTemplate.opsForList().size(listKey);

        if(size==null||size.intValue()==0)
            return null;

        Long incrementIndex = redisTemplate.opsForValue().increment(indexKey,1);

        if(incrementIndex>=size){
            redisTemplate.delete(indexKey);
        }

        Object showNum = redisTemplate.opsForList().index(listKey,incrementIndex-1);

        if(showNum==null)
            return null;


        return showNum.toString();

    }
}
